import axios from 'axios'
import { domain } from './config'

let isGet = true
isGet = false
const instance = axios.create()

let get1 = (url, params) => {
  return new Promise((resolve, reject) => {
    instance.get(`http://gxt.midea.com/test/times/opinion${url}`, {
      params
    })
      .then(resp => {
        resolve(resp.data)
      })
      .catch(error => {
        reject(error)
      })
  })
}

let get2 = (api, params) => {
  params = params || {}
  params.api = api
  const config = {
    headers: {'Content-Type': 'multipart/form-data'}
  }
  const formData = new FormData()
  for (let i in params) {
    formData.append(i, params[i])
  }
  return new Promise((resolve, reject) => {
    instance.post(`${domain}/dataService.do?command=spring:loadESApiMethodService&isApp=true`, formData, config)
      .then(resp => {
        resolve(resp.data)
      })
      .catch(error => {
        reject(error)
      })
  })
}

if (isGet) {
  get2 = get1
}

export const get = get2

export const getData = (params) => {
  const config = {
    headers: {'Content-Type': 'multipart/form-data'}
  }
  const formData = new FormData()
  for (let i in params) {
    formData.append(i, params[i])
  }
  return new Promise((resolve, reject) => {
    instance.post(`${domain}/dataService.do?command=spring:loadESApiMethodService&isApp=true`, formData, config)
      .then(resp => {
        resolve(resp.data)
      })
      .catch(error => {
        reject(error)
      })
  })
}

export default {
  get,
  getData
}
